package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.CarCardMapper;
import com.gxa.pingan.mapper.CarMapper;
import com.gxa.pingan.pojo.Car;
import com.gxa.pingan.pojo.CarCard;
import com.gxa.pingan.pojo.CarCardVo;
import com.gxa.pingan.service.CarCardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Author ChenJin
 * @date 2021/10/27 11:02
 * Description:
 **/
@Service("carCardService")
@Transactional(rollbackFor = {Throwable.class})
public class CarCardServiceImpl implements CarCardService {

    @Resource
    private CarCardMapper carCardMapper = null;

    @Resource
    private CarMapper carMapper = null;

    /**
     * 修改时，获取车主卡信息
     *
     * @param id 车主卡编号
     * @return
     */
    @Override
    public Map<String, Object> getCarCard(Long id) throws BusinessException {
        Map<String, Object> map = carCardMapper.selectById(id);
        // 设置时间格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String CLIVTAEndTime1 = dateFormat.format(map.get("CLIVTAEndTime"));
            String CLEndTime1 = dateFormat.format(map.get("CLEndTime"));
            String boardDate1 = dateFormat.format(map.get("boardDate"));
            String appTime1 = dateFormat.format(map.get("appTime"));
            String boardDate = boardDate1.substring(0, 19);
            String CLIVTAEndTime = CLIVTAEndTime1.substring(0, 19);
            String CLEndTime = CLEndTime1.substring(0, 19);
            String appTime = appTime1.substring(0, 19);
            map.put("CLIVTAEndTime",CLIVTAEndTime);
            map.put("CLEndTime",CLEndTime);
            map.put("boardDate",boardDate);
            map.put("appTime",appTime);
        Car car = carMapper.selectByCarNum(map.get("carNum").toString());
        System.out.println(car);
        if (car==null){
            throw new BusinessException(ExceptionCode.SERVICE_ERROR);
        }
        if (map==null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        return map;
    }

    /**
     * 添加车主卡
     *
     * @throws BusinessException
     */
    @Override
    public void insert(Map<String,Object> carCard) throws BusinessException {
        // 先判断手机号是否注册过
        Map<String, Object> map = carCardMapper.selectByPhone(carCard.get("phone").toString());
        Car car = carMapper.selectByCarNum(carCard.get("carNum").toString());
        if (car==null){
            carMapper.insert(carCard);
        }else {
            carMapper.update(carCard);
        }
        if (map!=null){
            throw new BusinessException(ExceptionCode.HAS_USER);
        }

        carCard.put("appTime",new Timestamp(System.currentTimeMillis()).toString());
        carCard.put("inUse",2);
        carCardMapper.insert(carCard);
    }

    /**
     * 修改车主卡信息
     *
     * @param carCard
     * @throws BusinessException
     */
    @Override
    public void update(Map<String,Object> carCard) throws BusinessException {
        System.out.println(carCard);
        CarCard getCarCard = carCardMapper.get(Long.valueOf(carCard.get("id").toString()));
        Car car = carMapper.selectByCarNum(carCard.get("carNum").toString());
        if (car==null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        // 先判断该车主卡是否存在
        if (getCarCard==null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        System.out.println(carCard.get("boardDate"));
        carMapper.update(carCard);
        carCardMapper.update(carCard);
    }

    /**
     * 获取车主卡总数
     *
     * @return
     * @throws BusinessException
     */
    @Override
    public Integer getListSize() throws BusinessException {
        List<Map<String, Object>> list = carCardMapper.getList();
        if (list.size()==0){
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return list.size();
    }

    /**
     * 分页获取车主卡列表
     *
     * @param page 页码
     * @param size 每页长度
     * @return
     * @throws BusinessException
     */
    @Override
    public List<Map<String, Object>> getList(Integer page, Integer size) throws BusinessException {
        List<Map<String, Object>> listByPage = carCardMapper.getListByPage((page - 1) * size, size);
        // 设置时间格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map<String,Object> map: listByPage) {
            String CLIVTAEndTime1 = dateFormat.format(map.get("CLIVTAEndTime"));
            String CLEndTime1 = dateFormat.format(map.get("CLEndTime"));
            String boardDate1 = dateFormat.format(map.get("boardDate"));
            String appTime1 = dateFormat.format(map.get("appTime"));
            String boardDate = boardDate1.substring(0, 19);
            String CLIVTAEndTime = CLIVTAEndTime1.substring(0, 19);
            String CLEndTime = CLEndTime1.substring(0, 19);
            String appTime = appTime1.substring(0, 19);
            map.put("CLIVTAEndTime",CLIVTAEndTime);
            map.put("CLEndTime",CLEndTime);
            map.put("boardDate",boardDate);
            map.put("appTime",appTime);
        }
        if (listByPage.size()==0){
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return listByPage;
    }

    /**
     * 不通过车主卡
     *
     * @param id 车主卡编号
     * @throws BusinessException
     */
    @Override
    public void noPass(Long id) throws BusinessException {
        Map<String, Object> map = carCardMapper.selectById(id);
        if (map==null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        Map<String, Object> map1 = carCardMapper.selectById(Long.valueOf(map.get("id").toString()));
        map1.put("inUse",1);
        carCardMapper.update(map1);
    }

    /**
     * 删除车主卡
     *
     * @param id
     *
     * @throws BusinessException
     */
    @Override
    public void delete(Long id) throws BusinessException {
        CarCard carCard = carCardMapper.get(id);
        if (carCard==null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        carCardMapper.delete(id);
    }

    /**
     * 模糊查询车主卡
     *
     * @param value 手机号、姓名的值
     * @param time  时间条件
     * @param page  页码
     * @param size  每页成都
     * @return
     * @throws BusinessException
     */
    @Override
    public List<Map<String, Object>> get(String value, Integer time, Integer page, Integer size) throws BusinessException {
        List<Map<String, Object>> list = carCardMapper.searchByPage(value, time, (page-1)*size, size);
        // 设置时间格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map<String,Object> map: list) {
            String boardDate1 = dateFormat.format(map.get("CLIVTAEndTime"));
            String CLIVTAEndTime1 = dateFormat.format(map.get("CLEndTime"));
            String CLEndTime1 = dateFormat.format(map.get("boardDate"));
            String appTime1 = dateFormat.format(map.get("appTime"));
            String boardDate = boardDate1.substring(0, 19);
            String CLIVTAEndTime = CLIVTAEndTime1.substring(0, 19);
            String CLEndTime = CLEndTime1.substring(0, 19);
            String appTime = appTime1.substring(0, 19);
            map.put("CLIVTAEndTime",CLIVTAEndTime);
            map.put("CLEndTime",CLEndTime);
            map.put("boardDate",boardDate);
            map.put("appTime",appTime);
        }
        // 先判断查出来的数据是否为空
        if (list.size()==0){
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return list;
    }

    /**
     * 获取查询除的总数量
     *
     * @param value
     * @param time
     * @return
     * @throws BusinessException
     */
    @Override
    public Integer getSize(String value, Integer time) throws BusinessException {
        List<Map<String, Object>> search = carCardMapper.search(value, time);
        if (search.size()==0){
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return search.size();
    }

    /**
     * 导出用户的数据
     *
     * @return
     * @throws BusinessException
     */
    @Override
    public List<CarCardVo> upload() throws BusinessException {
        List<CarCardVo> vo = carCardMapper.getVo();
        if (vo==null){
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return vo;
    }
}
